define(function(require, exports, module) {
    'use strict';
    
    var rk = require('rk');
    require('../gridView/widget');




    $.widget('rk.xsyReferCardView', $.rk.xsyReferGridView, {
        options: {},
        _create: function() {
            var me = this;
            var elem = me.element;
            var opt = me.options;

            elem.attr('wtype', 'xsyReferCardView');
            elem.attr('is-private', 'true');
        },
        bindData: function(ownerData) {
            var me = this;
            var elem = me.element;
            var opt = me.options;

            ownerData = $.extend(true, {}, ownerData);

            var data = {
                entitys: []
            };
            var itemList = ownerData.itemList;
            var entityData = ownerData.entityData;

            if (entityData.length == 0) {
                elem.html('没有数据');
                return;
            }

            me._processItemList(itemList);


            var MAX_ROWS = opt.maxRows;
            if (MAX_ROWS > entityData.length) MAX_ROWS = entityData.length;

            var maxCol = opt.maxCols;
            if (maxCol > itemList.length) maxCol = itemList.length;

            for (var i = 0, len = MAX_ROWS; i < len; i++) {
                var entitydata = entityData[i];
                entitydata.items = [];
                /*var cell = {
                    id: entitydata.id,
                    belongId: entitydata.belongId,
                    entityType: entitydata.entityType,
                    name: entitydata.name ? entitydata.name : '',
                    permissions: entitydata.permissions,
                    items: [],
                    entitydata : entitydata
                };*/
                var counter = 0;
                for (var j = 1, lenj = itemList.length; j < lenj; j++) {
                    var item = itemList[j];
                    var name = item.name;
                    if (name == 'name') continue;
                    if (counter >= (maxCol)) break;
                    var info = me._itemListCache[name];
                    entitydata.items.push({
                        name : item.name,
                        title: item.label,
                        formatText: me._getDisplayingHtml({}, JSON.stringify(info), entitydata, entitydata[name], item.itemType),
                        icon:entitydata[name]&&entitydata[name].icon?entitydata[name].label:''
                    });
                    counter++;
                }


                data.entitys.push(entitydata);
            }
            var tpl = opt.tpl ? opt.tpl : require('platform/widgets/crm/su/xsySuRefer/cardView/widget.tpl');
            var html = rk.templateText(tpl, {
                ownerConfig: opt.ownerConfig,
                data: data
            });
            elem.html(html);
        }
    });
});
